package cn.lili.trigger;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONUtil;
import cn.lili.cache.Cache;
import cn.lili.common.utils.SpringContextUtil;
import cn.lili.routing.PromotionRoutingKey;
import cn.lili.trigger.enums.DelayStatusEnums;
import cn.lili.trigger.model.TimeTriggerMsg;
import cn.lili.trigger.util.DelayQueueTools;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 事件触发消费者
 *
 * @author paulG
 * @since 2020/11/17 7:19 下午
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class TimeTriggerConsumer {
    private final Cache<String> cache;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = ("${lili.amqp.promotion}" + "_" + PromotionRoutingKey.PINTUAN)),
            exchange = @Exchange(value = "${lili.amqp.promotion}"),
            key = PromotionRoutingKey.PINTUAN))
    public void onMessage(TimeTriggerMsg timeTriggerMsg) {
        try {
            String key = DelayQueueTools.generateKey(timeTriggerMsg.getTriggerExecutor(), timeTriggerMsg.getTriggerTime(),
                    timeTriggerMsg.getUniqueKey());
            String sign = cache.get(key);
            if (CharSequenceUtil.isEmpty(sign)) {
                log.error("延时任务执行器缓存内容被清除，任务中止：{} | 任务标识：{}", timeTriggerMsg.getTriggerExecutor(), timeTriggerMsg.getUniqueKey());
                return;
            }
            if (sign.equals(DelayStatusEnums.STOP.name())) {
                log.info("延时任务被标记结束，任务中止：{} | 任务标识：{}", timeTriggerMsg.getTriggerExecutor(), timeTriggerMsg.getUniqueKey());
                return;
            }

            log.info("执行器执行：" + timeTriggerMsg.getTriggerExecutor());
            log.info("执行器参数：" + JSONUtil.toJsonStr(timeTriggerMsg.getParam()));

            cache.remove(key);

            TimeTriggerExecutor executor = (TimeTriggerExecutor) SpringContextUtil.getBean(timeTriggerMsg.getTriggerExecutor());
            executor.execute(timeTriggerMsg.getParam());
        } catch (Exception e) {
            log.error("mq延时任务异常", e);
        }

    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = ("${lili.amqp.promotion}" + "_" + PromotionRoutingKey.COUPON_ACTIVITY)),
            exchange = @Exchange(value = "${lili.amqp.promotion}"),
            key = PromotionRoutingKey.COUPON_ACTIVITY))
    public void onMessageCouponActivity(TimeTriggerMsg timeTriggerMsg) {
        try {
            String key = DelayQueueTools.generateKey(timeTriggerMsg.getTriggerExecutor(), timeTriggerMsg.getTriggerTime(),
                    timeTriggerMsg.getUniqueKey());
            String sign = cache.get(key);
            if (CharSequenceUtil.isEmpty(sign)) {
                log.error("延时任务执行器缓存内容被清除，任务中止：{} | 任务标识：{}", timeTriggerMsg.getTriggerExecutor(), timeTriggerMsg.getUniqueKey());
                return;
            }
            if (sign.equals(DelayStatusEnums.STOP.name())) {
                log.info("延时任务被标记结束，任务中止：{} | 任务标识：{}", timeTriggerMsg.getTriggerExecutor(), timeTriggerMsg.getUniqueKey());
                return;
            }

            log.info("执行器执行：" + timeTriggerMsg.getTriggerExecutor());
            log.info("执行器参数：" + JSONUtil.toJsonStr(timeTriggerMsg.getParam()));

            cache.remove(key);

            TimeTriggerExecutor executor = (TimeTriggerExecutor) SpringContextUtil.getBean(timeTriggerMsg.getTriggerExecutor());
            executor.execute(timeTriggerMsg.getParam());
        } catch (Exception e) {
            log.error("mq延时任务异常", e);
        }

    }

}
